package com.wang.vedio.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.view.WheelView;
import com.wang.vedio.R;
import com.wang.vedio.utils.StringUtils;

import java.util.List;

public class CommunitySelector {
    WheelView mWheelView;
    TextView mConfirmTxt;
    TextView mCancelTxt;
    TextView title;

    private Context context;
    private Activity activity;
    private Dialog dialog;
    private List<String> communities;
    private CallBack callBack;
    private WheelAdapter<String> adapter;
    private String label;

    public CommunitySelector(Context context,String label, List<String> communities, CallBack callBack) {
        this.context = context;
        this.activity = (Activity) context;
        this.communities = communities;
        this.callBack = callBack;
        this.label = label;
        initDialog(context);
        initView(context);
    }

    private void initDialog(Context context) {
        dialog = new Dialog(context, R.style.Dialog_Customer);
        setWindow1(dialog,activity);
        dialog.setContentView(R.layout.layout_community_seletor_dialog);
    }

    private void initView(Context context) {
        this.mWheelView = dialog.findViewById(R.id.wheel_view);
        mWheelView.setTextColorCenter(context.getResources().getColor(R.color.orange));
        mWheelView.setDividerColor(context.getResources().getColor(R.color.orange));
        if(!StringUtils.isEmpty(label)){
            mWheelView.setLabel(label);
        }
        this.mConfirmTxt = dialog.findViewById(R.id.confirm_txt);
        this.mCancelTxt = dialog.findViewById(R.id.cancel_txt);
        this.title = dialog.findViewById(R.id.title);
        adapter = new WheelAdapter<String>() {
            @Override
            public int getItemsCount() {
                return communities.size();
            }

            @Override
            public String getItem(int index) {
                return communities.get(index);
            }

            @Override
            public int indexOf(String o) {
                return 26;
            }
        };
        mWheelView.setAdapter(adapter);
        mConfirmTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.onConfirm(adapter.getItem(mWheelView.getCurrentItem()));
                }
                dialog.dismiss();
            }
        });
        mCancelTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    public void show() {
        dialog.show();
    }

    public void setCurrentView(String value){
        for(int i=0;i<communities.size();i++){
            if(value.equals(communities.get(i))){
                mWheelView.setCurrentItem(i);
                break;
            }
        }
    }

    public void setTitle(String titleInfo){
        title.setText(titleInfo);
    }

    public interface CallBack{
        void onConfirm(String checkInfo);
    }

    public void setWindow1(Dialog dialog, Activity activity) {
        Window window = dialog.getWindow();
        WindowManager wm = activity.getWindowManager();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        window.setWindowAnimations(R.style.dialogStyle);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
//        dialog.setContentView(view);
    }
}
